package com.reservation.service.impl;

import com.reservation.entity.Reservation;
import com.reservation.mapper.ReservationMapper;
import com.reservation.service.ReservationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
public class ReservationServiceImpl implements ReservationService {
    private static final Logger logger = LoggerFactory.getLogger(ReservationServiceImpl.class);
    private static final Random random = new Random();

    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public Reservation getReservationById(Integer rid) {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            logger.error("模拟服务异常，触发熔断，获取预订信息失败，ID: {}", rid);
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        try {
            return reservationMapper.getReservationById(rid);
        } catch (Exception e) {
            logger.error("Failed to get reservation by ID: {}", rid, e);
            return null;
        }
    }

    @Override
    public List<Reservation> getAllReservations() {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            logger.error("模拟服务异常，触发熔断，获取所有预订信息失败");
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        try {
            return reservationMapper.getAllReservations();
        } catch (Exception e) {
            logger.error("Failed to get all reservations", e);
            return null;
        }
    }

    @Override
    @Transactional
    public boolean insertReservation(Reservation reservation) {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            logger.error("模拟服务异常，触发熔断，插入预订信息失败: {}", reservation);
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        try {
            int result = reservationMapper.insertReservation(reservation);
            return result > 0;
        } catch (Exception e) {
            logger.error("Failed to insert reservation: {}", reservation, e);
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateReservation(Reservation reservation) {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            logger.error("模拟服务异常，触发熔断，更新预订信息失败: {}", reservation);
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        try {
            int result = reservationMapper.updateReservation(reservation);
            return result > 0;
        } catch (Exception e) {
            logger.error("Failed to update reservation: {}", reservation, e);
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteReservation(Integer rid) {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            logger.error("模拟服务异常，触发熔断，删除预订信息失败，ID: {}", rid);
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        try {
            int result = reservationMapper.deleteReservation(rid);
            return result > 0;
        } catch (Exception e) {
            logger.error("Failed to delete reservation by ID: {}", rid, e);
            return false;
        }
    }
}